package com.jianneng.jdg.model.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.com.jianneng.jdg.R;
import com.jianneng.jdg.model.user.ReleaseInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyReleaseAdapter extends BaseAdapter {
	private List<ReleaseInfo> list = new ArrayList<ReleaseInfo>();
	private LayoutInflater mInflater;
	private Context context;
	
	public MyReleaseAdapter(Context context,List<ReleaseInfo> list) {
		this.list = list;
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mHolder = null;
		//解决抢占焦点问题
//		ViewGroup vg = (ViewGroup) convertView;
//		vg.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		if(convertView == null){
			mHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.myrelease_listview_item, null);
			mHolder.myrelease_image = (ImageView) convertView.findViewById(R.id.myrelease_image);
			mHolder.release_username = (TextView) convertView.findViewById(R.id.release_username);
			mHolder.release_content = (TextView) convertView.findViewById(R.id.release_content);
			mHolder.delete_content = (ImageView) convertView.findViewById(R.id.delete_content);
			convertView.setTag(mHolder);
		}else{
			mHolder = (ViewHolder) convertView.getTag();
		}
		final ReleaseInfo info = list.get(position);
		if(position==0){
			mHolder.myrelease_image.setImageResource(R.drawable.dialogue_message);
		}else if(position==1){
			mHolder.myrelease_image.setImageResource(R.drawable.notification_message);
		}
		
		mHolder.release_username.setText(info.username);
		mHolder.release_content.setText(info.repease_content);
		mHolder.delete_content.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(context, position+"被刪除了", 1).show();
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
	private static class ViewHolder{
		ImageView myrelease_image;
		TextView release_username;
		TextView release_content;
		ImageView delete_content;
	}

}
